
/**
 * Main application class and also the main window
 * @author Narendra J. Mondal
 * @date 15.01.2012
 */

package plotter;

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.FileFilter;

public class SurfPlot extends JFrame
{
	// used for exporting
	private JFileChooser chooser = new JFileChooser();
	
	// dialog for editing the surface
	private EditDialog ed;
	
	// where the surface will be rendered
	private PlotPanel p = new PlotPanel();
	
	public SurfPlot() {
		super("SurfPlot");
		setSize(800, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		createMenus();
		
		add(p);
		
		// do not show all files in chooser
		chooser.setAcceptAllFileFilterUsed(false);
		
		// use native look
		try {
			String lf = UIManager.getSystemLookAndFeelClassName();
			UIManager.setLookAndFeel(lf);
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void createMenus() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu file = new JMenu("File");
		file.setMnemonic('F');
		
		JMenuItem edit = new JMenuItem("Edit Surface");
		edit.setToolTipText("Edit surface properties");
		edit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (ed == null)
					ed = new EditDialog(SurfPlot.this, p);
				ed.setVisible(true);
			}
		});
		file.add(edit);
		
		JMenuItem export = new JMenuItem("Export to PNG");
		export.setToolTipText("Export the surface as a PNG image");
		export.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				chooser.setFileFilter(new PNGFilter());
				int ret = chooser.showSaveDialog(SurfPlot.this);
				if (ret == JFileChooser.APPROVE_OPTION)
					try {
						String fname = chooser.getSelectedFile().getAbsolutePath()+".png";
						p.export(new File(fname));
					} catch (IOException ie) {
						
						JOptionPane.showMessageDialog(SurfPlot.this, ie.getMessage(),
								"SurfPlot", JOptionPane.ERROR_MESSAGE);
					}
			}
		});

		file.add(export);
		
		file.addSeparator();
		
		JMenuItem exit = new JMenuItem("Exit");
		exit.setToolTipText("Quit SurfPlot");
		exit.addActionListener(new  ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		file.add(exit);
		
		menuBar.add(file);
				
		JMenu help = new JMenu("Help");
		help.setMnemonic('H');
		
		JMenuItem about = new JMenuItem("About");
		about.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AboutDialog ab=new AboutDialog(SurfPlot.this);
				if (ab != null)
					ab.setVisible(true);
			}
		});
		help.add(about);
		
		menuBar.add(help);
		
		setJMenuBar(menuBar);
	}
	public static void main(String[] args) {
		SurfPlot sp = new SurfPlot();
		sp.setVisible(true);
	}
	
}
/**
 * Used to show only PNG files in the file chooser dialog
 * @author Narendra J. Mondal
 *
 */
class PNGFilter extends FileFilter
{
	public boolean accept(File file) 
	{
		return file.isDirectory() | file.getName().endsWith("png") | file.getName().endsWith("PNG");
	}
	public String getDescription() 
	{
		return "PNG Image";
	}
	
}